package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.common.util.IpUtil;
import com.cskaoyan.wordmemorize.dto.admin.DeleteEmployeeRoleOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.EmployeeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.EmployeeCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.EmployeeService;
import com.cskaoyan.wordmemorize.service.FileUploadService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/employee")
public class AdminEmployeeController {

    @Autowired
    EmployeeService employeeService;
    // GET ***获取当前当前登录用户信息
    @Autowired
    FileUploadService fileUploadService;

    @GetMapping("info")
    public Result<EmployeeDTO> getEmployeeInfo() {
        // 获取当前登录的用户id
        Long employeeId = StpKit.ADMIN.getLoginIdAsLong();


        EmployeeDTO employeeInfo = employeeService.getEmployeeInfo(employeeId);

        return Result.ok(employeeInfo);
    }
    // POST **后台添加员工
    @PostMapping("")
    public Result addEmployee(@RequestBody EmployeeCommand employeeCommandnd,HttpServletRequest request) {


        employeeService.addEmployee(employeeCommandnd);
        return Result.ok();


    }


    // PUT **后台修改员工信息
    @PutMapping("")
    public Result updateEmployee(@RequestBody EmployeeCommand employeeCommand) {
        employeeService.updateEmployee(employeeCommand);
        return Result.ok();
    }
    // POST ****上传头像
    @PostMapping("avatar")
    public Result<String> uploadAvatar(MultipartFile file) {
        try {
            String avatar = fileUploadService.upload(file);
            return Result.ok(avatar);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    // GET **获取后台员工列表
    @GetMapping("list")
    public Result<PageDTO<EmployeeDTO>> getEmployeePage(PageRequest request) {
        PageDTO<EmployeeDTO> employeePage = employeeService.getEmployeePage(request);
        return Result.ok(employeePage);

    }

    // DELETE **后台删除员工信息
    @SaAdminCheckPermission("admin:employee:delete")
    @DeleteMapping("{employeeId}")
    public Result deleteEmployee(@PathVariable("employeeId") Long employeeId) {
        employeeService.delete(employeeId);
        return Result.ok();
    }
    //DELETE **后台删除员工-角色关联关系
    @DeleteMapping("/{employeeId}/role/{roleId}")
    public Result deleteEmployeeRole(@PathVariable("employeeId") Long employeeId, @PathVariable("roleId") Long roleId)
    {
        DeleteEmployeeRoleOperateDTO deleteEmployeeRoleOperateDTO = employeeService.deleteRole(employeeId, roleId);
        return Result.ok(deleteEmployeeRoleOperateDTO);

    }
}